package easyjava.hibernate;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

import easyjava.hibernate.bean.Student;

public class ProjectionDemo {

	public static void main(String[] args) {

		
	       try{
	        	  SessionFactory sf=new Configuration().configure().buildSessionFactory();
	        	  Session session =sf.openSession();
	        	  
	        	  Criteria criteria =session.createCriteria(Student.class);
	        
	        	  ProjectionList projList=Projections.projectionList();
	        	  projList.add(Projections.countDistinct("id"));
	        	  projList.add(Projections.max("phone"));
	        	  projList.add(Projections.groupProperty("age"));
	        	  
	        	  criteria.setProjection(projList);
	        	  
	        	  List<Student> students =criteria.list();
	        	  for(Object obj :students){
	        		  System.out.println(Arrays.toString((Object[])obj));
	        	  }
	       }catch(HibernateException e){
	        		  e.printStackTrace();
	        		  }
	        	  }

}
